package com.news.ui;

import com.news.model.NewsItem;

import javax.swing.*;
import java.awt.*;
import java.time.format.DateTimeFormatter;

/**
 * 新闻项目列表渲染器
 */
public class NewsItemRenderer extends DefaultListCellRenderer {
    
    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("MM-dd HH:mm");
    
    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        
        if (value instanceof NewsItem) {
            NewsItem item = (NewsItem) value;
            
            // 设置显示内容
            String displayText = formatNewsItem(item);
            setText(displayText);
            
            // 设置颜色
            if (isSelected) {
                setBackground(new Color(0, 123, 255));
                setForeground(Color.WHITE);
            } else {
                setBackground(Color.WHITE);
                setForeground(new Color(51, 51, 51));
            }
            
            // 设置边框和间距
            setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(230, 230, 230)),
                BorderFactory.createEmptyBorder(8, 10, 8, 10)
            ));
            
            // 设置字体
            setFont(new Font("微软雅黑", Font.PLAIN, 12));
        }
        
        return this;
    }
    
    /**
     * 格式化新闻项目显示文本
     */
    private String formatNewsItem(NewsItem item) {
        StringBuilder html = new StringBuilder();
        html.append("<html>");
        html.append("<div style='width: 350px;'>");
        
        // 标题
        html.append("<div style='font-weight: bold; font-size: 13px; margin-bottom: 4px;'>");
        html.append(truncateText(item.getTitle(), 50));
        html.append("</div>");
        
        // 来源和时间
        html.append("<div style='font-size: 11px; color: #666; margin-bottom: 4px;'>");
        html.append(item.getSource());
        if (item.getPublishTime() != null) {
            html.append(" · ");
            html.append(item.getPublishTime().format(TIME_FORMATTER));
        }
        html.append(" · 热度: ");
        html.append(item.getHotScore());
        html.append("</div>");
        
        // 内容摘要
        if (item.getContent() != null && !item.getContent().trim().isEmpty()) {
            html.append("<div style='font-size: 11px; color: #888;'>");
            html.append(truncateText(item.getContent(), 80));
            html.append("</div>");
        }
        
        html.append("</div>");
        html.append("</html>");
        
        return html.toString();
    }
    
    /**
     * 截断文本
     */
    private String truncateText(String text, int maxLength) {
        if (text == null) return "";
        if (text.length() <= maxLength) {
            return text;
        }
        return text.substring(0, maxLength) + "...";
    }
} 